package com.miaopasi.securitydemo.config.security;

import lombok.Data;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

/**
 * 用户信息
 *
 * @author lixin
 */
@Data
public class SysUser implements UserDetails, CredentialsContainer {
    /*** 记录id（数据库自增） */
    private Long id;
    /*** 创建时间（创建设置） */
    private Date gmtCreate;
    /*** 修改时间（创建时可以不填入，每次修改必须设置） */
    private Date gmtModified;
    /*** 逻辑删除（true表示被逻辑删除） */
    private Boolean isDelete;
    /*** 操作人（每次操作更新操作人） */
    private String operator;
    /*** 排序字段 */
    private BigDecimal sort;
    /*** 乐观锁字段，每次更新记录+1 */
    private Long version;
    /*** 用户名 */
    private String username;
    /*** 密码 */
    private String password;
    /*** 用户状态 */
    private Integer status;
    /*** 备注 */
    private String remarks;
    /*** 权限集合 */
    private Collection<GrantedAuthority> authorities;

    @Override
    public void eraseCredentials() {
        this.password = null;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 0;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status != 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status != 2;
    }

    @Override
    public boolean isEnabled() {
        return this.status != -1;
    }
}
